//
//  AddFolderViewController.m
//  SecretFolder
//
//  Created by Deepak Kumar on 24/06/12.
//  Copyright 2012 Frog. All rights reserved.
//

#import "AddFolderViewController.h"
#import "Repository.h"
#import "Directory.h"

@implementation AddFolderViewController
@synthesize mFolderNameField, mFolderNameLabel, mFolderImage, mDelegate;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	mCurrentFolderIndex = 0;
	
	self.navigationItem.title = @"Add Folder";
	
	self.navigationItem.rightBarButtonItem =
	[[UIBarButtonItem alloc] initWithTitle:@"Save"
									  style:UIBarButtonItemStyleDone
									 target:self
									 action:@selector(saveButton:)];
	
	self.navigationItem.leftBarButtonItem =
	[[UIBarButtonItem alloc] initWithTitle:@"Cancel"
									  style:UIBarButtonItemStyleBordered
									 target:self
									 action:@selector(cancelButton:)];
    NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica" size:19],UITextAttributeFont, nil];
    self.navigationController.navigationBar.titleTextAttributes = size;
	
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if ([self.mFolderNameLabel.text length]) 
		self.mFolderNameLabel.text = self.mFolderNameField.text;
	
	[self.mFolderNameField resignFirstResponder];

	return YES;
}

- (IBAction) moreButton:(id)sender
{
	
	if ([self.mFolderNameLabel.text length]) 
		self.mFolderNameLabel.text = self.mFolderNameField.text;
	
	[self.mFolderNameField resignFirstResponder];
	
	FolderListViewController *tController = [[FolderListViewController alloc] initWithNibName:@"FolderListViewController" bundle:nil];
	[tController setMDelegate:self];
	[self.navigationController pushViewController:tController animated:YES];
}

- (void) folderListViewController:(FolderListViewController *)pController index:(NSInteger)pIndex
{
	mCurrentFolderIndex = pIndex;
	self.mFolderImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"IT_Folder%d.png", pIndex]];
}

- (IBAction) saveButton:(id)sender
{
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
	NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

	NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:self.mFolderNameField.text];
	
	if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
	{	
		NSError *error;
		[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
        

        NSManagedObjectContext *tContext = [Repository sharedRepository].context;
        Directory *tDirectory = [NSEntityDescription
                           insertNewObjectForEntityForName:@"Directory"
                           inManagedObjectContext:tContext];

        tDirectory.mName = self.mFolderNameField.text;
        tDirectory.mFolderType = [NSNumber numberWithInteger:mCurrentFolderIndex];
        if ([[NSUserDefaults standardUserDefaults] boolForKey:kDecoyPasswordEntered]== YES) {
            tDirectory.mDecoyFolder = [NSNumber numberWithBool:YES];
        }
        [[Repository sharedRepository].context insertObject:tDirectory];
        [[Repository sharedRepository].context save:nil];
        
		[self cancelButton:sender];
		[mDelegate addFolderViewController:self refresh:YES];

	}
	else {
		UIAlertView *iAlert = [[UIAlertView alloc] initWithTitle:@"Folder already existing, try some another name." message:nil delegate:nil 
								cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[iAlert show];
	}	
}

- (IBAction) cancelButton:(id)sender
{
	[self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Override to allow orientations other than the default portrait orientation.
    return UIInterfaceOrientationIsLandscape(interfaceOrientation)? YES : NO;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.mFolderNameField = nil;
	self.mFolderNameLabel = nil;
	self.mFolderImage = nil;
}




@end
